package student.collection.list;

import java.util.ArrayList;

/**
   ArrayList线程非安全，数组越界的异常，值为null
 */
public class ArrayListTest {

    public static void main(String[] args) {

        ArrayList<String> arrayList = new ArrayList<>();

        new Thread(() -> {
            for (int i = 1; i <= 1000; i++) {
                arrayList.add(i + "");
            }
        }, Thread.currentThread().getName()).start();

        new Thread(() -> {
            for (int i = 1001; i < 2000; i++) {
                arrayList.add(i + "");
            }
        }, Thread.currentThread().getName()).start();


        // 打印所有结果
        System.out.println("元素总共：" + arrayList.size() + "个");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println("第" + (i + 1) + "个元素为：" + arrayList.get(i));
        }
    }
}
